<template>
  <div class="register-container">
    <div class="register-content">
      <div class="register-box">
        <div class="register-header">
          <h2>用户注册</h2>
          <p>欢迎来到河南省体育与艺术用品招标采购平台</p>
        </div>
        <el-form :model="registerForm" :rules="rules" ref="registerFormRef" label-width="0px">
          <el-form-item prop="username">
            <el-input v-model="registerForm.username" placeholder="请输入用户名" prefix-icon="el-icon-user"></el-input>
          </el-form-item>
          <!-- <el-form-item prop="email">
            <el-input
              v-model="registerForm.email"
              placeholder="请输入邮箱"
              prefix-icon="el-icon-message"
            ></el-input>
          </el-form-item> -->
          <el-form-item prop="phone">
            <el-input v-model="registerForm.phone" placeholder="请输入手机号" prefix-icon="el-icon-mobile"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input type="password"
              v-model="registerForm.password"
              placeholder="请输入密码"
              prefix-icon="el-icon-lock"
              autocomplete="new-password"
              show-password></el-input>
          </el-form-item>
          <el-form-item prop="confirmPassword">
            <el-input type="password" v-model="registerForm.confirmPassword" placeholder="请确认密码"
              prefix-icon="el-icon-lock" show-password></el-input>
          </el-form-item>
          <el-form-item prop="captcha">
            <div class="captcha-container">
              <el-input v-model="registerForm.captcha" placeholder="请输入验证码" prefix-icon="el-icon-key"></el-input>
              <div class="captcha-image">
                <img src="../assets/captcha.svg" alt="验证码" @click="refreshCaptcha" />
              </div>
            </div>
          </el-form-item>
          <el-form-item prop="agreement">
            <el-checkbox v-model="registerForm.agreement">
              我已阅读并同意
              <a href="#" class="agreement-link" @click.prevent>《用户服务协议》</a>
              和
              <a href="#" class="agreement-link" @click.prevent>《隐私政策》</a>
            </el-checkbox>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" class="register-button" @click="handleRegister">注册</el-button>
          </el-form-item>
          <el-form-item>
            <div class="login-link">
              已有账号？<router-link to="/login">立即登录</router-link>
            </div>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'

const router = useRouter()

// 表单数据
const registerForm = ref({
  username: '',
  phone: '',
  password: '',
  confirmPassword: '',
  captcha: '',
  agreement: false
})

// 表单引用
const registerFormRef = ref(null)

// 密码验证规则
const validatePassword = (rule, value, callback) => {
  if (value === '') {
    callback(new Error('请输入密码'))
  } else if (value.length < 6) {
    callback(new Error('密码长度不能小于6位'))
  } else {
    if (registerForm.value.confirmPassword !== '' && registerFormRef.value) {
      registerFormRef.value.validateField('confirmPassword')
    }
    callback()
  }
}

// 确认密码验证规则
const validateConfirmPassword = (rule, value, callback) => {
  if (value === '') {
    callback(new Error('请再次输入密码'))
  } else if (value !== registerForm.value.password) {
    callback(new Error('两次输入密码不一致'))
  } else {
    callback()
  }
}

// 表单验证规则
const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
  ],
  phone: [
    { required: true, message: '请输入手机号', trigger: 'blur' },
    { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号', trigger: 'blur' }
  ],
  password: [
    { validator: validatePassword, trigger: 'blur' }
  ],
  confirmPassword: [
    { validator: validateConfirmPassword, trigger: 'blur' }
  ],
  captcha: [
    { required: true, message: '请输入验证码', trigger: 'blur' },
    { min: 4, max: 4, message: '验证码为 4 位字符', trigger: 'blur' }
  ],
  agreement: [
    {
      validator: (rule, value, callback) => {
        if (!value) {
          callback(new Error('请阅读并同意用户协议'))
        } else {
          callback()
        }
      },
      trigger: 'change'
    }
  ]
}

// 注册处理
const handleRegister = () => {
  registerFormRef.value.validate((valid) => {
    if (valid) {
      console.log('Registering with:', registerForm.value)
      ElMessage.success('注册成功')
      router.push('/login')
    }
  })
}

// 刷新验证码
const refreshCaptcha = () => {
  console.log('Refreshing captcha')
}
</script>

<style scoped>
.register-container {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

.register-content {
  flex: 1;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 40px 0;
  background-color: #f5f7fa;
}

.register-box {
  width: 400px;
  padding: 30px;
  background-color: white;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.register-header {
  text-align: center;
  margin-bottom: 30px;
}

.register-header h2 {
  font-size: 24px;
  color: #333;
  margin-bottom: 10px;
}

.register-header p {
  font-size: 14px;
  color: #999;
}

.captcha-container {
  display: flex;
  gap: 10px;
}

.captcha-image {
  flex-shrink: 0;
  width: 120px;
  height: 40px;
  overflow: hidden;
  border: 1px solid #dcdfe6;
  border-radius: 4px;
  cursor: pointer;
}

.captcha-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.agreement-link {
  color: #409eff;
  text-decoration: none;
}

.register-button {
  width: 100%;
}

.login-link {
  text-align: center;
  font-size: 14px;
  color: #666;
}

.login-link a {
  color: #409eff;
  text-decoration: none;
}
</style>
